﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace hozorghiab
{
    public partial class user_tabs : Form
    {
        public sql sqlquery;
        public user_tabs(sql sqlinput)
        {
            InitializeComponent();
            sqlquery = sqlinput;
        }

        private void user_tabs_Load(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if ((textBox8.Text != textBox7.Text) || (textBox8.Text == "" || textBox7.Text == ""))
            {
                MessageBox.Show("password is not match");
            }
            else
            {
                try
                {
                    sqlquery.open();
                    sqlquery.ExecuteNonQuery(@"sp_addlogin '" + textBox9.Text.Trim() + "','" + textBox8.Text.Trim() + "'");
                    //sqlquery.ExecuteNonQuery(@"sp_grantlogin '" + textBox9.Text.Trim() + "'");
                    sqlquery.ExecuteNonQuery(@"sp_grantdbaccess '" + textBox9.Text.Trim() + "'");
                    if (BulkAdmin.Checked == true){
                        sqlquery.ExecuteNonQuery(@"sp_addsrvrolemember '" + textBox9.Text.Trim() + "','bulkadmin'");
                    }
                    if (DiskAdmin.Checked == true)
                    {
                        sqlquery.ExecuteNonQuery(@"sp_addsrvrolemember '" + textBox9.Text.Trim() + "','diskadmin'");
                    }
                    if (SecurityAdmin.Checked == true)
                    {
                        sqlquery.ExecuteNonQuery(@"sp_addsrvrolemember '" + textBox9.Text.Trim() + "','securityadmin'");
                    }
                    if (ServerAdmin.Checked == true)
                    {
                        sqlquery.ExecuteNonQuery(@"sp_addsrvrolemember '" + textBox9.Text.Trim() + "','serveradmin'");
                    }
                    if (SetupAdmin.Checked == true)
                    {
                        sqlquery.ExecuteNonQuery(@"sp_addsrvrolemember '" + textBox9.Text.Trim() + "','setupadmin'");
                    }
                    if (SysAdmin.Checked == true)
                    {
                        sqlquery.ExecuteNonQuery(@"sp_addsrvrolemember '" + textBox9.Text.Trim() + "','sysadmin'");
                    }
                    if (DbCreator.Checked == true)
                    {
                        sqlquery.ExecuteNonQuery(@"sp_addsrvrolemember '" + textBox9.Text.Trim() + "','dbCreator'");
                    }
                    sqlquery.close();
                    MessageBox.Show("عملیات با موفقیت انجام شد");
                    textBox9.Text = "";
                    textBox8.Text = "";
                    textBox7.Text = "";
                    BulkAdmin.Checked = false;
                    DiskAdmin.Checked = false;
                    SecurityAdmin.Checked = false;
                    ServerAdmin.Checked = false;
                    SetupAdmin.Checked = false;
                    SysAdmin.Checked = false;
                    DbCreator.Checked = false;
                }
                catch
                {
                    MessageBox.Show("خطایی رخ داده است دوباره امتحان کنید");
                }
            }
            textBox9.Text = "";
            textBox8.Text = "";
            textBox7.Text = "";
            BulkAdmin.Checked = false;
            DiskAdmin.Checked = false;
            SecurityAdmin.Checked = false;
            ServerAdmin.Checked = false;
            SetupAdmin.Checked = false;
            SysAdmin.Checked = false;
            DbCreator.Checked = false;
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void ex_btn2_Click_1(object sender, EventArgs e)
        {
            Close();
        }

        private void delete_user_Click_1(object sender, EventArgs e)
        {
            try
            {
                sqlquery.open();
                sqlquery.ExecuteNonQuery(@"sp_droplogin '" + textBox10.Text + "'");
                sqlquery.close();
                MessageBox.Show("عملیات با موفقیت انجام شد");
                textBox1.Text = "";
            }
            catch
            {
                MessageBox.Show("خطایی رخ داده است دوباره امتحان کنید");
            }
            textBox4.Text = "";
        }

        private void change_pass_Click_1(object sender, EventArgs e)
        {
            if ((newpass.Text != renewpass.Text) || (renewpass.Text == "" || newpass.Text == "" || oldpass.Text == ""))
            {
                MessageBox.Show("password is not match");
            }
            else
            {
                try
                {
                    sqlquery.open();
                    sqlquery.ExecuteNonQuery(@"sp_password '" + oldpass.Text + "','" + newpass.Text + "," + id_change_pass.Text + "'");
                    sqlquery.close();
                    MessageBox.Show("عملیات با موفقیت انجام شد");
                    oldpass.Text = "";
                    newpass.Text = "";
                    renewpass.Text = "";
                    id_change_pass.Text = "";
                }
                catch
                {
                    MessageBox.Show("خطایی رخ داده است دوباره امتحان کنید");
                }
            }
            oldpass.Text = "";
            newpass.Text = "";
            renewpass.Text = "";
            id_change_pass.Text = "";
        }

        private void ex_btn3_Click(object sender, EventArgs e)
        {
            Close();
        }



    }
}
